import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/utils/kwargs.dart';
import 'package:flutter_admin/utils/odoo_http_util.dart';
import 'package:fluttertoast/fluttertoast.dart';

class PurchaseEvaluationProjectOdooApi {
  static Future<ResponseBodyApi> page({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      PageModel pageModel = data['page'];
      List domainList = data['domainList'];
      var fieldsInfo = {
        'purchase.evaluation.template': [
          'template_id',
          'template_name',
        ],
      };
      Kwargs recordsKwargs = Kwargs(
          domain: domainList,
          limit: pageModel.size,
          offset: (pageModel.current - 1) * pageModel.size,
          order: pageModel.orders[0].column! +
              " " +
              (pageModel.orders[0].asc! ? "asc" : "desc"));
      var records = await OdooHttpUtil.callKw(
        model: 'purchase.evaluation.project',
        method: 'search_data',
        kwargs: recordsKwargs.toMap(),
        fieldsInfo: fieldsInfo,
      );
      var total = await OdooHttpUtil.callKw(
          model: 'purchase.evaluation.project',
          method: 'search_count',
          kwargs: {"domain": domainList});
      List<Map<String, dynamic>> list = [];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        list.add(map);
      });
      pageModel.records = list;
      pageModel.total = total;
      pageModel.pages = total ~/ pageModel.size + 1;
      responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> removeByIds(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      Kwargs recordsKwargs = Kwargs(domain: data);
      var res = await OdooHttpUtil.callKw(
          model: 'purchase.evaluation.project',
          method: "unlink_project_data",
          kwargs: recordsKwargs.toMap());
      if (res['code'] == 200) {
        // CryUtils.message(res["message"]);
        responseBodyApi = ResponseBodyApi(success: true);
      } else {
        CryUtils.message(res["message"]);
        responseBodyApi = ResponseBodyApi(success: false);
      }
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> saveOrUpdate(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      if (data["id"] == null) {
        Kwargs recordsKwargs = Kwargs(domain: [
          {
            'template_id': data['template_id'],
            'evaluation_project': data['evaluation_project'],
            'dept_id': data['dept_id'],
            'comment': data['comment'],
            'tester_id': data['tester_id']
          }
        ]);
        var res = await OdooHttpUtil.callKw(
            model: 'purchase.evaluation.project',
            method: 'create_project_data',
            kwargs: recordsKwargs.toMap());
        if (res['code'] == 200) {
          responseBodyApi = ResponseBodyApi(success: true);
        } else {
          CryUtils.message(res["message"]);
          responseBodyApi = ResponseBodyApi(success: false);
        }
      } else {
        await OdooHttpUtil.callKw(
            model: 'purchase.evaluation.project',
            method: 'write',
            args: [
              [data["id"]],
              data
            ]);
        responseBodyApi = ResponseBodyApi(success: true);
      }
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  ///
  /// 获取模板关联考评人的记录
  ///@Author: chendeji
  ///@Date: 2022-05-25 15:08:28
  ///
  static getTemplateTesterItems({data}) async {
    Fluttertoast.showToast(msg: '请求模板关系表信息');
    ResponseBodyApi responseBodyApi;
    try {
      PageModel pageModel = data['page'];
      Kwargs recordsKwargs = Kwargs(
          domain: [],
          limit: pageModel.size,
          offset: (pageModel.current - 1) * pageModel.size,
          order: pageModel.orders[0].column! +
              " " +
              (pageModel.orders[0].asc! ? "asc" : "desc"));
      var records = await OdooHttpUtil.callKw(
        model: 'purchase.supplier.evaluator',
        method: 'search_data',
        kwargs: recordsKwargs.toMap(),
      );
      var total = await OdooHttpUtil.callKw(
          model: 'purchase.supplier.evaluator',
          method: 'search_count_new',
          kwargs: {"domain": []});
      List<Map<String, dynamic>> list = [];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        list.add(map);
      });
      pageModel.records = list;
      pageModel.total = total;
      pageModel.pages = total ~/ pageModel.size + 1;
      responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }
}
